package com.butterfly.hotkey.core.util;

import com.butterfly.hotkey.core.rule.ClientKeyRule;
import org.springframework.util.PatternMatchUtils;

import java.util.List;

/**
 * @author butterfly
 * @version 1.0
 */
public class KeyRuleUtil {

    /**
     * 查询key关联的规则信息
     *
     * @param key            key
     * @param clientKeyRules keyRules
     * @return KeyRule
     */
    public static ClientKeyRule findKeyRule(String key, List<ClientKeyRule> clientKeyRules) {
        for (ClientKeyRule clientKeyRule : clientKeyRules) {
            String keyPattern = clientKeyRule.getKeyPattern();
            boolean match = PatternMatchUtils.simpleMatch(keyPattern, key);
            if (match) {
                return clientKeyRule;
            }
        }
        return null;
    }
}
